package cc.imos.service;

import cc.imos.communal.constant.Const;
import cc.imos.communal.entity.Page;
import cc.imos.communal.err.ErrCode;
import cc.imos.communal.err.ErrException;
import cc.imos.entity.SysAccount;
import cc.imos.entity.SysAccountLine;
import cc.imos.medium.AccountPageArgs;
import cc.imos.medium.LoginRest;
import cc.imos.repository.SystemAccountRepository;
import cc.imos.medium.LoginArgs;
import com.xiaoleilu.hutool.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 * 账号服务层实现
 *
 * Created by asher on 2017/3/4.
 * </pre>
 */
@Slf4j
@Service
public class SystemAccountService {

    @Autowired
    private SystemAccountRepository systemAccountRepository;

    /**
     * 登录功能
     * @param loginArgs 登录参数 <br />
     * @return 账号信息
     */
    public LoginRest login(LoginArgs loginArgs) throws ErrException {
        SysAccount sysAccount = null;
        try {
            sysAccount = systemAccountRepository.findForLogin(loginArgs);
        } catch (DataAccessException e) {
            log.error("登录检索对象失败", e);
            throw new ErrException(ErrCode.xxx);
        }
        String accessToken = RandomUtil.randomNumbers(Const.DEFAULT_INT_30);
        String accessKey = RandomUtil.randomNumbers(Const.DEFAULT_INT_50);
        Long expiresTime = System.currentTimeMillis() + Const.DEFAULT_EXPIRES_SECOND_MILLI;

        SysAccountLine sysAccountLine = new SysAccountLine();
        sysAccountLine.setAccessToken(accessToken);
        sysAccountLine.setAccessKey(accessKey);
        sysAccountLine.setExpiresTime(expiresTime);
        sysAccountLine.setAccountId(sysAccount.getId());
        sysAccountLine.setLineMold(Const.DEFAULT_ON_LINE_WEB);
        systemAccountRepository.insertLine(sysAccountLine);

        LoginRest loginRest = new LoginRest(accessToken, accessKey, expiresTime, null);
        return loginRest;
    }

    public void logout(SysAccountLine sysAccountLine) throws ErrException {
        systemAccountRepository.logout(sysAccountLine);
    }

    /**
     * 分页账号数据
     *
     * @param args 参数
     * @return 分页 对象
     * @throws ErrException 异常
     */
    public Page<Map<String, Object>> page(AccountPageArgs args) throws ErrException {
        Page page = systemAccountRepository.page(args);
        return page;
    }

    /**
     * 新增账号
     *
     * @param paras 参数
     * @return 账号
     * @throws ErrException 异常
     */
    public Map<String, Object> add(Map<String, Object> paras) throws ErrException {
        Map<String, Object> checkParas = new HashMap<>();
        checkParas.put("name", paras.get("name"));
        check(checkParas); // 检查是否可用
        long id = systemAccountRepository.insert(paras);
        paras.put("id", id);
        return paras;
    }

    /**
     * 检查是否可用
     *
     * @param paras 检查参数
     * @throws ErrException 不可用时抛出异常
     */
    public void check(Map<String, Object> paras) throws ErrException {
    }

    /**
     * 查看指定账号下得角色
     *
     * @param args 条件
     * @return 结果
     * @throws ErrException 异常
     */
    public List<Map<String, Object>> listRole(Map<String, Object> args) throws ErrException {
        return systemAccountRepository.listRole(args);
    }
}
